package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Favorite;
import com.example.domain.Rating;
import com.example.domain.Review;
import com.example.domain.User;
import com.example.domain.model.UserDTO;
import com.example.mapper.FavoriteMapper;
import com.example.mapper.RatingMapper;
import com.example.mapper.ReviewMapper;
import com.example.service.UserService;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
* @author Administrator
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-03-20 09:46:36
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private RatingMapper ratingMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public User getById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public User getByName(String userName) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername,userName));
    }

    @Override
    public IPage<UserDTO> getlist(Page page) {
        return userMapper.selectPage(page, null);
    }

    @Override
    public boolean register(User user) {
        User user1 = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, user.getUsername()));
        //判断用户名是否已存在
        if (user1 == null){
            user.setIsAdmin(0);
            user.setIsActive(1);
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            return userMapper.insert(user) > 0;
        }else {
            return false;
        }
    }

    @Override
    public boolean update(String id, User user) {
        User u = userMapper.selectById(id);
        if (u != null){
            user.setId(id);
            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            }else {
                user.setPassword(u.getPassword());
            }
            return userMapper.updateById(user) > 0;
        }else {
            return false;
        }
    }

    @Override
    public boolean delete(String id) {
        favoriteMapper.delete(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId,id));
        ratingMapper.delete(new LambdaQueryWrapper<Rating>().eq(Rating::getUserId,id));
        reviewMapper.delete(new LambdaQueryWrapper<Review>().eq(Review::getUserId,id));
        return userMapper.deleteById(id) > 0;
    }

}




